/**
 * Created by gongxun on 16/11/21.
 */

var KwxTableOperatingLayer = GameCCSBase.extend({
    Panel_normal: null,
    Panel_kou : null,
    mNewActionList : null,
    Button_guo : null,
    Button_hu : null,
    Button_liang : null,
    Button_gang : null,
    Button_peng : null,
    Panel_selectGang : null,
    Image_selectGangs : null,
    mGangControls : null,

    //是否打开了亮操作
    mLiangSelect : null,
    //扣牌背景
    Image_kouBgs : null,
    //选择扣的牌
    mKouSelectNums : null,
    //扣完之后可以打的牌
    mLiangPlayNums : null,
    //确定扣牌
    Button_confromLiang : null,
    //取消扣牌
    Button_cancelLiang : null,
    //亮牌提示信息
    Image_liangTishi : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/KwxTableOperating.json");
        //吃碰杠胡
        this.Panel_normal = mo.seekWidgetByName(this.ccsNode, "Panel_normal");
        this.Panel_kou = mo.seekWidgetByName(this.ccsNode, "Panel_kou");

        this.Button_guo = mo.seekWidgetByName(this.Panel_normal, "Button_guo");
        this.Button_hu = mo.seekWidgetByName(this.Panel_normal, "Button_hu");
        this.Button_liang = mo.seekWidgetByName(this.ccsNode, "Button_liang");
        this.Button_gang = mo.seekWidgetByName(this.Panel_normal, "Button_gang");
        this.Button_peng = mo.seekWidgetByName(this.Panel_normal, "Button_peng");
        this.Panel_selectGang = mo.seekWidgetByName(this.ccsNode, "Panel_selectGang");
        this.Panel_selectGang.setVisible(false);
        this.Image_liangTishi = mo.seekWidgetByName(this.ccsNode, "Image_liangTishi");
        this.Image_liangTishi.setVisible(false);

        this.loadUI();

        this.setVisible(false);
    },

    loadUI: function () {
        //ting
        this.Button_liang.addTouchEventListener(this.onTouchLiang, this);

        //guo
        this.Button_guo.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.Panel_selectGang.setVisible(false);
                this.sendOpenControl([8]);
            }
        }, this);

        //hu
        this.Button_hu.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.Panel_selectGang.setVisible(false);
                this.sendOpenControl([3]);
            }
        }, this);

        //gang
        this.Button_gang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendOpenControlGang();
            }
        }, this);

        //peng
        this.Button_peng.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.Panel_selectGang.setVisible(false);
                this.sendOpenControl([2]);
            }
        }, this);

        //确认扣底
        this.Button_confromLiang = mo.seekWidgetByName(this.ccsNode, "Button_confromLiang");
        this.Button_confromLiang.setPositionX(mo.gameSize.x / 2 - 75);
        this.Button_confromLiang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.startLiangDealCard();
            }
        }, this);

        //取消扣抵
        this.Button_cancelLiang = mo.seekWidgetByName(this.ccsNode, "Button_cancelLiang");
        this.Button_cancelLiang.setPositionX(mo.gameSize.x / 2 + 75);
        this.Button_cancelLiang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.startLiang();
                this.startLiangDealCard();
            }
        }, this);

        //Image_kouBgs
        this.Image_kouBgs = [];
        for (var i = 0; i < 4; i++){
            var Image_kouBg = mo.seekWidgetByName(this.ccsNode, "Image_kouBg" + i);
            Image_kouBg.setContentSize(mo.kwxCardHelper.kouSelectBoxSize);
            Image_kouBg.addTouchEventListener(this.onTouchKouSelectButton , this);

            //添加拍背
            var Panel_cardBack = mo.seekWidgetByName(Image_kouBg, "Panel_cardBack");
            for (var j = 0; j < 3; j++){
                var card = new KwxCard();
                card.setCardNum(0);
                card.setCardKind(0, 4);
                Panel_cardBack.addChild(card);

                card.setScale(mo.kwxCardHelper.inhandCardScale);
                card.setPosition(cc.p((mo.kwxCardHelper.getInhnadCardSize().width + mo.kwxCardHelper.inhandCardOffset) * j + mo.kwxCardHelper.getInhnadCardSize().width / 2, 0));
            }

            this.Image_kouBgs.push(Image_kouBg);
        }

        //杠的多种情况
        this.Image_selectGangs = [];
        for (var i = 0; i < 4; i++){
            var card = new KwxCard();
            card.setName("card");

            var Image_selectGang = mo.seekWidgetByName(this.Panel_selectGang, "Image_selectGang" + i);
            Image_selectGang.addChild(card);
            Image_selectGang.setTag(i);
            Image_selectGang.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var index = sender.getTag();
                    var actionRequestkwx = {};
                    actionRequestkwx.code = this.mGangControls[index].code;
                    actionRequestkwx.Cards1 = [this.mGangControls[index].card];
                    mo.kwxTableStatus.sendOpenCard(actionRequestkwx);
                    cc.log("操作 code:" + actionRequestkwx.code);
                }
            }, this);

            this.Image_selectGangs.push(Image_selectGang);
        }

        this.Panel_selectGang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.Panel_selectGang.setVisible(false);
            }
        }, this);
    },

    //设置是否显示自己的操作
    showOperating : function(isShow, newActionList){
        this.setVisible(isShow);
        this.Panel_selectGang.setVisible(false);
        this.mNewActionList = newActionList;
        if (isShow){
            this.Panel_normal.setVisible(true);
            this.Panel_kou.setVisible(false);

            var showBtns = [];
            showBtns = this.setOperatingButton(this.Button_peng, [2], showBtns);
            showBtns = this.setOperatingButton(this.Button_gang, [4, 5, 6], showBtns);
            showBtns = this.setOperatingButton(this.Button_liang, [0], showBtns);
            showBtns = this.setOperatingButton(this.Button_hu, [3], showBtns);
            showBtns = this.setOperatingButton(this.Button_guo, [8], showBtns);
            var offsetX = 150;
            var startPx = mo.gameSize.x / 2 - (showBtns.length - 1) / 2 * offsetX;
            for (var i = 0; i < showBtns.length; i++){
                showBtns[i].setPositionX(startPx + offsetX * i);
            }

            this.setLiangButtonSelect(false);
        }
    },

    setOperatingButton : function(button, codes, btns){
        var isShow = false;
        for (var i = 0; i < codes.length; i++){
            for(var j = 0; j < this.mNewActionList.length; j++){
                var code = this.mNewActionList[j].code;
                if (code == codes[i]){
                    isShow = true;
                    if (code == 0){
                        if (this.mNewActionList[j].Cg == null){
                            isShow = false;
                            continue;
                        }
                    }
                    break;
                }
            }
        }

        if (isShow){
            button.setVisible(true);
            btns.push(button);
        }else{
            button.setVisible(false);
        }
        return btns;
    },

    //发送操作
    sendOpenControl : function(codes){
        for (var i = 0; i < codes.length; i++){
            for(var j = 0; j < this.mNewActionList.length; j++){
                var code = this.mNewActionList[j].code;
                if (code == codes[i]){
                    mo.kwxTableStatus.sendOpenCard(this.mNewActionList[j]);
                    cc.log("操作 code:" + code);
                    break;
                }
            }
        }
    },

    //发送杠操作
    sendOpenControlGang : function(){
        var codes = [4, 5, 6];

        this.mGangControls = [];
        for(var j = 0; j < this.mNewActionList.length; j++){
            var code = this.mNewActionList[j].code;
            if (code == 4){
                //明杠直接打出
                mo.kwxTableStatus.sendOpenCard(this.mNewActionList[j]);
            }else if (code == 5 || code == 6){
                //暗杠和续杠让用户选择
                for (var i = 0; i < this.mNewActionList[j].Cards1.length; i++){
                    var gangJiegou = {};
                    gangJiegou.code = code;
                    gangJiegou.card = this.mNewActionList[j].Cards1[i];
                    this.mGangControls.push(gangJiegou);
                }
            }
        }

        if (this.mGangControls.length == 1){
            var actionRequestkwx = {};
            actionRequestkwx.code = this.mGangControls[0].code;
            actionRequestkwx.Cards1 = [this.mGangControls[0].card];
            mo.kwxTableStatus.sendOpenCard(actionRequestkwx);
            cc.log("操作 code:" + actionRequestkwx.code);
        }else{
            this.Panel_selectGang.setVisible(true);
            for(var i = 0; i < 4; i++){
                var isShow = i < this.mGangControls.length;
                this.Image_selectGangs[i].setVisible(isShow);
                if (isShow){
                    var scale = 80 / mo.kwxCardHelper.inhandCardSize.width;
                    var cardSize = cc.size(mo.kwxCardHelper.inhandCardSize.width * scale, mo.kwxCardHelper.inhandCardSize.height * scale);
                    var bgSize = this.Image_selectGangs[i].getContentSize();
                    var p = cc.p(bgSize.width / 2, bgSize.height / 2 - cardSize.height / 2);

                    var card = mo.seekWidgetByName(this.Image_selectGangs[i], "card");
                    card.setCardNum(this.mGangControls[i].card);
                    card.setCardKind(0, 3);
                    card.setScale(scale);
                    card.setPosition(p);
                }
            }
        }
    },

    //返回亮结构
    getLiangAction : function(){
        var liangAction = null;
        for (var i = 0; i < this.mNewActionList.length; i++){
            if (this.mNewActionList[i].code == 0){
                liangAction = this.mNewActionList[i];
                break;
            }
        }
        return liangAction;
    },

    //返回可以扣的牌
    getKouCardNums : function(useCardNums){
        var kouCardNums = [];
        //顺便计算扣完牌之后可以打得牌
        this.mLiangPlayNums = [];

        var liangAction = this.getLiangAction();
        var openTingWGS = liangAction.Cg;
        for (var i = 0; i < openTingWGS.Wgss.length; i++){
            //废牌对应的赢的组合
            var winGroups = openTingWGS.Wgss[i];
            // var winGroup = openTingWGS.Wgss[i];

            for (var wgindex = 0; wgindex < winGroups.Wg.length; wgindex++){
                //这个是一种所有的胡牌结果
                var winGroup = winGroups.Wg[wgindex];

                //当前赢的组合的所有可以扣的牌
                var tempKouNums = [];

                for (var CgIndex = 0; CgIndex < winGroup.Cg.length; CgIndex++){
                    var cg = winGroup.Cg[CgIndex];
                    if (cg.GroupType == 2 && cg.Lznum == 0){
                        tempKouNums.push(cg.Cards[0]);
                    }
                }

                //tempKouNums是否全部包含useCardNums
                //如果是 把不包含的插入kouCardNums
                //如果否 break;
                var findNumsArray = mo.gameHelper.getContainArrayAndReturnLeft(tempKouNums, useCardNums);
                if (findNumsArray[0]){
                    //找到了
                    kouCardNums = kouCardNums.concat(findNumsArray[1]);

                    //包含了
                    this.mLiangPlayNums.push(openTingWGS.Cs[i]);
                }else{
                    //没找到
                }
            }
        }

        kouCardNums = mo.gameHelper.getUniqueArray(kouCardNums);

        //亮听
        this.mLiangPlayNums = mo.gameHelper.getUniqueArray(this.mLiangPlayNums);
        this.setTingFlag(this.mLiangPlayNums);

        return kouCardNums;
    },

    //计算亮的时候听的牌
    getTingTimeHuCardNum : function(liangCardNum){
        if (liangCardNum == null){
            //取消
            mo.kwxTable.mKwxTableUILayer.setMyShowTing([]);
        }else{
            //设置
            var canHuCardNums = [];

            var liangAction = this.getLiangAction();
            var openTingWGS = liangAction.Cg;
            //找到这个结构里面所有赖子对应的手牌
            var winGroups = null;
            for (var i = 0; i < openTingWGS.Cs.length; i++){
                if (openTingWGS.Cs[i] == liangCardNum){
                    winGroups = openTingWGS.Wgss[i];
                    break;
                }
            }
            if (winGroups){
                for(var i = 0; i < winGroups.Wg.length; i++){
                    var winGroup = winGroups.Wg[i];

                    //过滤掉不包含扣的牌
                    var gangCards = [];
                    for (var j = 0; j < winGroup.Cg.length; j++){
                        if (winGroup.Cg[j].GroupType == 2 && winGroup.Cg[j].Lznum == 0){
                            gangCards.push(winGroup.Cg[j].Cards[0]);
                        }
                    }
                    var iscontainSelectKou = mo.gameHelper.getContainArrayAndReturnLeft(gangCards, this.mKouSelectNums);
                    if (!iscontainSelectKou[0]){
                        continue;
                    }

                    for (var j = 0; j < winGroup.Cg.length; j++){
                        var cardGroupkwx = winGroup.Cg[j];
                        if (cardGroupkwx.Lznum == 1){
                            //处理
                            var lzCardNums = mo.kwxCardHelper.getLzDecryptCardNums(cardGroupkwx.GroupType, cardGroupkwx.Cards, cardGroupkwx.Lznum);
                            canHuCardNums = canHuCardNums.concat(lzCardNums);
                        }
                    }
                }
            }

            canHuCardNums = mo.gameHelper.getUniqueArray(canHuCardNums);
            canHuCardNums.sort();
            mo.kwxTable.mKwxTableUILayer.setMyShowTing(canHuCardNums);
        }
    },

    //亮
    onTouchLiang : function(sender, type){
        if (mo.gameHelper.getIsTouchEnd(type)) {
            this.Panel_selectGang.setVisible(false);

            if (this.mLiangSelect == false){
                this.startLiang();
                this.setLiangButtonSelect(true);
            }else{
                this.stopLiang();
                this.setLiangButtonSelect(false);
            }
        }
    },

    //设置是否打开了亮
    setLiangButtonSelect : function(isLiang){
        this.mLiangSelect = isLiang;

        if (this.mLiangSelect){
            this.Button_liang.loadTextureNormal("res/ccs/table/operating/guo_n.png");
            this.Button_liang.setContentSize(this.Button_liang.getNormalTextureSize());
            this.Button_liang.setScale(1);
        }else{
            this.Button_liang.loadTextureNormal("res/ccs/table/operating/liang_n.png");
            this.Button_liang.setContentSize(this.Button_liang.getNormalTextureSize());
            this.Button_liang.setScale(1);
        }
        this.Button_liang.setAnchorPoint(cc.p(0.5, 0.5));

        //亮提示
        this.Image_liangTishi.setVisible(isLiang);
    },

    stopLiang : function(){
        this.Panel_normal.setVisible(true);
        this.Button_liang.setVisible(true);
        this.Panel_kou.setVisible(false);

        //通知停止监听停牌
        mo.kwxTableStatus.stopUseTing();

        //取消染黑手牌
        mo.kwxTable.mKwxCardLayer.mPlayers[0].setTingOperatingBlack([]);

        //停止显示可以胡的牌
        this.getTingTimeHuCardNum();

        //停止显示听的牌
        this.setTingFlag(mo.kwxTableStatus.getMyTingNums());

        //放下所有手牌
        mo.kwxTable.mKwxCardLayer.mPlayers[0].onTouchCardBegan(cc.p(-1000, -1000));
    },

    startLiang : function(){
        this.Panel_normal.setVisible(false);
        this.Button_liang.setVisible(false);
        this.Panel_kou.setVisible(true);
        this.Panel_kou.setTouchEnabled(true);
        this.setkouBgTouch(true);
        this.Button_confromLiang.setVisible(true);
        this.Button_cancelLiang.setVisible(true);

        //亮的提示1
        mo.gameHelper.setRootTitleText(this.Image_liangTishi, "亮牌的扣牌阶段，请选择需要扣下的牌");

        this.mKouSelectNums = [];
        this.mLiangPlayNums = [];

        //手牌强制排序
        mo.kwxTable.mKwxCardLayer.mPlayers[0].sortInahndCard();

        //选择框全部关闭
        for (var i = 0; i < this.Image_kouBgs.length; i++){
            this.Image_kouBgs[i].setVisible(false);
        }

        var kouCardNums = this.getKouCardNums(this.mKouSelectNums);
        if (kouCardNums.length > 0){
            this.showKouBaoji(kouCardNums);
        }else{
            this.startLiangDealCard();
        }
    },

    //找到可以扣的牌 并且标记
    showKouBaoji : function(kouCardNums){
        var cards = mo.kwxTable.mKwxCardLayer.mPlayers[0].inhandCards;
        var allKouCards = this.mKouSelectNums.concat(kouCardNums);
        allKouCards = mo.gameHelper.getUniqueArray(allKouCards);

        //选择框全部关闭
        for (var i = 0; i < this.Image_kouBgs.length; i++){
            this.Image_kouBgs[i].setVisible(false);
        }

        for (var i = 0; i < allKouCards.length; i++){
            var index = mo.kwxCardHelper.findCardForNum(cards, allKouCards[i]);
            var pos = cc.p(cards[index].getBoundingBox().x, cards[index].getBoundingBox().y);
            this.showKouSelectImage(i, allKouCards[i], pos);
        }

        //标记可以选择的牌 其他牌变黑
        var blackNums = [];
        var cards = mo.kwxTable.mKwxCardLayer.mPlayers[0].inhandCards;
        for (var i = 0; i < cards.length; i++){
            var cardNum = cards[i].mCardNum;

            var findCard = false;
            for (var j = 0; j < kouCardNums.length; j++) {
                if (kouCardNums[j] == cardNum){
                    //找到了
                    findCard = true;
                    break;
                }
            }

            if (!findCard){
                blackNums.push(cardNum);
            }
        }
        mo.kwxTableStatus.useKouTing = true;
        var grayCardNums = mo.kwxTable.mKwxCardLayer.mPlayers[0].setTingOperatingBlack(blackNums);
        mo.kwxTableStatus.useKouTing = false;
    },

    showKouSelectImage : function(index, kouCardNum, pos){
        this.Image_kouBgs[index].setVisible(true);
        this.Image_kouBgs[index].setPosition(pos);
        this.Image_kouBgs[index].setTag(kouCardNum);

        var isSelect = false;
        for (var i = 0; i < this.mKouSelectNums.length; i++){
            if (this.mKouSelectNums[i] == kouCardNum){
                isSelect = true;
                break;
            }
        }
        mo.seekWidgetByName(this.Image_kouBgs[index], "Panel_cardBack").setVisible(isSelect);

        //更新确定扣牌显示
        if (this.mKouSelectNums.length > 0){
            this.Button_confromLiang.loadTextureNormal("res/ccs/table/operating/koupai_n.png");
            this.Button_confromLiang.setTouchEnabled(true);
        }else{
            this.Button_confromLiang.loadTextureNormal("res/ccs/table/operating/koupai_s.png");
            this.Button_confromLiang.setTouchEnabled(false);
        }
    },

    onTouchKouSelectButton : function(sender, type){
        if (mo.gameHelper.getIsTouchEnd(type)) {
            var cardNum = sender.getTag();

            var isSelect = false;
            var findIndex = 0;
            for (var i = 0; i < this.mKouSelectNums.length; i++){
                if (this.mKouSelectNums[i] == cardNum){
                    isSelect = true;
                    findIndex = i;
                    break;
                }
            }

            if (isSelect){
                //取消操作
                this.mKouSelectNums.splice(findIndex, 1);
            }else{
                //添加操作
                this.mKouSelectNums.push(cardNum);
            }

            var kouCardNums = this.getKouCardNums(this.mKouSelectNums);
            this.showKouBaoji(kouCardNums);
        }
    },

    //亮最后一步 打开手牌
    startLiangDealCard : function(){
        this.Panel_normal.setVisible(false);
        this.Button_liang.setVisible(true);
        this.Panel_kou.setVisible(true);
        this.Panel_kou.setTouchEnabled(false);
        this.setkouBgTouch(false);
        this.Button_confromLiang.setVisible(false);
        this.Button_cancelLiang.setVisible(false);

        //亮的提示2
        mo.gameHelper.setRootTitleText(this.Image_liangTishi, "亮牌确认阶段，打出一张废牌后完成亮牌");

        //通知可以打出废牌了
        mo.kwxTableStatus.useTing = true;
        mo.kwxTableStatus.useTingKouNums = this.mKouSelectNums;

        //黑掉不是废牌的牌
        var blackNums = [];
        var cards = mo.kwxTable.mKwxCardLayer.mPlayers[0].inhandCards;
        for (var i = 0; i < cards.length; i++){
            var cardNum = cards[i].mCardNum;

            var findCard = false;
            for (var j = 0; j < this.mLiangPlayNums.length; j++) {
                if (this.mLiangPlayNums[j] == cardNum){
                    //找到了
                    findCard = true;
                    break;
                }
            }

            if (!findCard){
                blackNums.push(cardNum);
            }
        }
        var grayCardNums = mo.kwxTable.mKwxCardLayer.mPlayers[0].setTingOperatingBlack(blackNums);

        //计算能否打出废牌 不能的话提示
        var myInhandCardNums = [];
        for (var i = 0; i < mo.kwxTable.mKwxCardLayer.mPlayers[0].inhandCards.length; i++){
            myInhandCardNums.push(mo.kwxTable.mKwxCardLayer.mPlayers[0].inhandCards[i].mCardNum);
        }
        myInhandCardNums = mo.gameHelper.getUniqueArray(myInhandCardNums);
        grayCardNums =  mo.gameHelper.getUniqueArray(grayCardNums);
        if (mo.gameHelper.getContainArrayAndReturnLeft(grayCardNums, myInhandCardNums)[0]){
            //亮的提示3
            mo.gameHelper.setRootTitleText(this.Image_liangTishi, "不能亮牌，没有合适的牌打出去");
        }
    },

    //设置听牌的flag
    setTingFlag : function(tingNums){
        mo.kwxTable.mKwxCardLayer.mPlayers[0].mFlagTingNums = tingNums;
        mo.kwxTable.mKwxCardLayer.mPlayers[0].setInhandCardFlag();
    },

    //设置扣牌背景可以触摸
    setkouBgTouch : function(isTouch){
        for (var i = 0; i < this.Image_kouBgs.length; i++) {
            this.Image_kouBgs[i].setTouchEnabled(isTouch);
        }
    }
});